package com.modifiertech.lineage.handler;

import com.modifiertech.lineage.api.PlayerData;
import com.modifiertech.lineage.event.PlayerManaModifyEvent;
import net.minecraft.client.entity.EntityPlayerSP;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.fml.common.FMLCommonHandler;
import java.util.concurrent.ConcurrentHashMap;

/**用户魔力变更处理器
 *
 */
public class PlayerManaModifyHandler {
    //饿汉
    public static PlayerManaModifyHandler INSTANCE=new PlayerManaModifyHandler();
    private PlayerManaModifyHandler(){
    }
    public ConcurrentHashMap<String,Integer> playerTickCalculate = new ConcurrentHashMap<>();
    int maxInternal = 20;


    /**变更魔力
     *
     * @param entityPlayer
     * @param modifyMana 变更的魔力值
     * @param data
     */
    public void manaModify(EntityPlayer entityPlayer,double modifyMana,PlayerData data){
        MinecraftForge.EVENT_BUS.post(new PlayerManaModifyEvent.Pre(entityPlayer,modifyMana));
        boolean modifyManaStatus=false;
            //处理魔力变化
        modifyManaStatus=data.playerManaModify(modifyMana);
        if(modifyManaStatus){
            //数据保存/同步
            //固定间隔进行数据同步
            FMLCommonHandler.instance().getMinecraftServerInstance().addScheduledTask(()->{
                data.saveAndSyncSilent();
            });
        }
        MinecraftForge.EVENT_BUS.post(new PlayerManaModifyEvent.Post(entityPlayer,modifyMana));
    }
    public void manaModifyTick(EntityPlayer entityPlayer,double modifyMana,PlayerData data){
        timesPlus(entityPlayer);
        MinecraftForge.EVENT_BUS.post(new PlayerManaModifyEvent.Pre(entityPlayer,modifyMana));
        boolean modifyManaStatus=false;
        //处理魔力变化
        modifyManaStatus=data.playerManaModify(modifyMana);
        if(modifyManaStatus){
            //数据保存/同步
            //固定间隔进行数据同步
            if (timesCheckMax(entityPlayer)){
                timesReset(entityPlayer);
                FMLCommonHandler.instance().getMinecraftServerInstance().addScheduledTask(()->{
                    data.saveAndSyncSilent();
                });
            }
            MinecraftForge.EVENT_BUS.post(new PlayerManaModifyEvent.Post(entityPlayer,modifyMana));
        }else{
            MinecraftForge.EVENT_BUS.post(new PlayerManaModifyEvent.PostErr(entityPlayer,modifyMana));
        }
    }


    /**次数累计
     *
     * @param entityPlayer
     */
    public void timesPlus(EntityPlayer entityPlayer){
        String fullKey =key(entityPlayer);
        if (!playerTickCalculate.containsKey(fullKey)){
            playerTickCalculate.put(fullKey,1);//初始化累计
        }else{
            playerTickCalculate.put(fullKey,playerTickCalculate.get(fullKey)+1);//累计
        }
    }

    /**检查是否到最大次数
     *
     * @param entityPlayer
     * @return
     */
    public boolean timesCheckMax(EntityPlayer entityPlayer){
        if (playerTickCalculate.containsKey(key(entityPlayer))
                && maxInternal== playerTickCalculate.get(key(entityPlayer))){
            return true;
        }
        return false;
    }

    /**重置次数
     *
     * @param entityPlayer
     */
    public void timesReset(EntityPlayer entityPlayer){
        String fullKey =key(entityPlayer);
        if (playerTickCalculate.containsKey(fullKey)){
            playerTickCalculate.put(fullKey,0);
        }
    }



    public String key(EntityPlayer entityPlayer){
        int key = entityPlayer == null ? 0 : entityPlayer.getUniqueID().hashCode();
        String prefix = entityPlayer instanceof EntityPlayerMP ?"mp_"
                :entityPlayer instanceof EntityPlayerSP ?"sp_":"uk_";
        String fullKey = prefix+key;
        return fullKey;
    }



}
